package id.ac.amikom.amikomsocial;

import id.ac.amikom.libs.AdemServiceHelper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AdemDetailMateriActivity extends Activity {

	private JSONArray jsonArray;
	private String key;

	TextView viewJudul;
	TextView viewDosen;
	TextView viewMkl;
	TextView viewFile;
	TextView viewDate;
	TextView viewDetail;
	TextView viewSize;
	ImageView viewImage;

	String title = "";
	String dosen = "";
	String mkl = "";
	String file = "";
	String date = "";
	String detail = "";
	String size = "";
	String fl = "";

	String id;

	private class GetDataTask extends AsyncTask<Void, Void, Boolean> {

		ProgressDialog dialog = new ProgressDialog(
				AdemDetailMateriActivity.this);
		boolean result = false;

		@Override
		protected Boolean doInBackground(Void... params) {

			AdemServiceHelper srv = new AdemServiceHelper(
					AdemDetailMateriActivity.this);
			jsonArray = srv.getDetailMateri(key);
			result = true;

			return result;

		}

		protected void onPreExecute() {
			this.dialog.setMessage("Loading Materi..");
			this.dialog.show();
		}

		@Override
		protected void onPostExecute(Boolean result) {
			this.dialog.dismiss();

			if (result)
				setContent();
			else {
				finish();
			}
		}

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.adem_materi);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.adem_title);

		Bundle extras = getIntent().getExtras();
		key = extras.getString("_key");
		id = extras.getString("_id");

		viewJudul = (TextView) findViewById(R.id.materi_title);
		viewDosen = (TextView) findViewById(R.id.materi_dosen);
		viewMkl = (TextView) findViewById(R.id.materi_mkl);
		viewFile = (TextView) findViewById(R.id.materi_file);
		viewDate = (TextView) findViewById(R.id.materi_date);
		viewDetail = (TextView) findViewById(R.id.materi_detail);
		viewSize = (TextView) findViewById(R.id.materi_size);
		viewImage = (ImageView) findViewById(R.id.materi_image);

		Button btnDown = (Button) findViewById(R.id.btnDownload);

		new GetDataTask().execute();

		btnDown.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {

				String url = "http://dosen.amikom.ac.id/downloads/materi/" + fl;
				Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
				startActivity(i);

				Toast.makeText(
						AdemDetailMateriActivity.this,
						"Starting download file, check status on notification bar",
						Toast.LENGTH_LONG).show();

			}
		});

	}

	private void setContent() {

		try {

			JSONObject json = jsonArray.getJSONObject(0);
			title = json.getString("judul");
			dosen = json.getString("nama");
			mkl = json.getString("mkl");
			file = json.getString("namafile");
			date = json.getString("tgl");
			detail = json.getString("sinopsis");
			size = json.getString("fsize");

			String[] f = file.split("[/]");
			String t[] = f[2].split("[.]");
			String type = t[t.length - 1];
			fl = file;

			viewJudul.setText(title);
			viewDosen.setText("Lecturer. " + dosen);
			viewMkl.setText("Subject. " + mkl);
			viewFile.setText("File. " + f[2]);
			viewDate.setText("Date. " + date);
			viewSize.setText("Size. " + size);
			viewDetail.setText("Sinopsis. " + detail);

			if (type.equals("doc") || type.equals("docx"))
				viewImage.setImageResource(R.drawable.i_word);
			else if (type.equals("xls") || type.equals("xlsx"))
				viewImage.setImageResource(R.drawable.i_excel);
			else if (type.equals("ppt") || type.equals("pptx"))
				viewImage.setImageResource(R.drawable.i_ppt);
			else if (type.equals("pdf"))
				viewImage.setImageResource(R.drawable.i_pdf);
			else
				viewImage.setImageResource(R.drawable.i_other);

		} catch (JSONException e) {
			e.printStackTrace();

		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.adem_menu_explore, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.id_exp_subj:
			Intent i = new Intent(AdemDetailMateriActivity.this,
					AdemExploreMateriActivity.class);
			i.putExtra("param", mkl);
			i.putExtra("mode", "mkl");
			i.putExtra("_id", id);
			startActivity(i);
			finish();
			return true;

		case R.id.id_exp_lect:
			Intent in = new Intent(AdemDetailMateriActivity.this,
					AdemExploreMateriActivity.class);
			in.putExtra("param", dosen);
			in.putExtra("mode", "dosen");
			in.putExtra("_id", "" + id);
			startActivity(in);
			finish();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}

	}

}
